package com.lxm.shownews.detail;

import android.os.Bundle;
import android.os.Parcelable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import com.lxm.shownews.R;
import com.lxm.shownews.bean.News;

/**
 * Created by lxm on 2016/2/29.
 */
public class DetailActivity extends AppCompatActivity {

    private News news;
    private WebView webView;
    private TextView webTitle;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_detail);
        news = getIntent().getExtras().getParcelable("news");
        webView = (WebView) findViewById(R.id.webView);
        webTitle = (TextView) findViewById(R.id.web_title);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient());
        webView.setWebViewClient(new WebViewClient());
        webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        if (news != null) {
            webTitle.setText(news.getTitle());
//            webView.loadDataWithBaseURL(null, news.getLink(), "text/html", "utf-8", null);
            webView.loadUrl(news.getLink());
        }

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (webView.canGoBack()) {
                webView.goBack();
                return true;
            }
        }
        return super.onKeyDown(keyCode, event);
    }
}
